namespace RivercrabNet
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Drawing;
    using System.Drawing.Drawing2D;

    public class IndicatorWVAD : BaseIndicator
    {
        private LineShape tlsMWvad;
        private LineShape tlsWvad;
        private string vdField = ("WVAD.VdField." + Common.GetGuid());

        public IndicatorWVAD()
        {
            base.IndType = IndicatorType.WVAD;
            base.IndName = "WVAD";
            base.IndDesc = "威廉变异离散量";
            base.IndPeriod.Add(new IndicatorPeriod(0, "P1", 24.0));
            base.IndPeriod.Add(new IndicatorPeriod(1, "P2", 6.0));
            base.SourceDic["Volume"] = "";
            base.SourceDic["Close"] = "";
            base.SourceDic["Open"] = "";
            base.SourceDic["High"] = "";
            base.SourceDic["Low"] = "";
            base.fields = new string[] { "WVAD", "MWVAD" };
        }

        public override bool Contains(object obj)
        {
            if ((obj != this.tlsWvad) && (obj != this.tlsMWvad))
            {
                return false;
            }
            return true;
        }

        public override double[] Get(int r)
        {
            double num = Convert.ToDouble(base.Crab.DataSource.Rows[r][base.SourceDic["Close"]]);
            double num2 = Convert.ToDouble(base.Crab.DataSource.Rows[r][base.SourceDic["Open"]]);
            double num3 = Convert.ToDouble(base.Crab.DataSource.Rows[r][base.SourceDic["High"]]);
            double num4 = Convert.ToDouble(base.Crab.DataSource.Rows[r][base.SourceDic["Low"]]);
            double num5 = Convert.ToDouble(base.Crab.DataSource.Rows[r][base.SourceDic["Volume"]]);
            double num6 = num - num2;
            double num7 = num3 - num4;
            double num8 = 0.0;
            if (num7 != 0.0)
            {
                num8 = (num6 / num7) * num5;
            }
            double curValue = num8;
            int num10 = r - (((int) base.IndPeriod[0].ParamValue) - 1);
            if (num10 < 0)
            {
                num10 = 0;
            }
            for (int i = r - 1; i >= num10; i--)
            {
                curValue += Convert.ToDouble(base.Crab.DataSource.Rows[i][this.vdField]);
            }
            double num12 = Common.CalcuteMovingAvg(r, (int) base.IndPeriod[1].ParamValue, this.tlsMWvad.Name, this.tlsWvad.Name, curValue, base.Crab.DataSource);
            return new double[] { num8, curValue, num12 };
        }

        public override List<BaseShape> GetShapeList()
        {
            return new List<BaseShape> { this.tlsWvad, this.tlsMWvad };
        }

        public override void Remove()
        {
            base.Crab.DeleteShape(this.TlsWvad);
            base.Crab.DeleteShape(this.TlsMWvad);
            base.Crab.DataSource.Columns.Remove(this.VdField);
        }

        public override bool Set(int r, string target)
        {
            try
            {
                DataRow row = base.Crab.DataSource.Rows[r];
                foreach (string str in base.SourceDic.Values)
                {
                    if ((str == target) || (target == null))
                    {
                        foreach (string str2 in base.SourceDic.Values)
                        {
                            if (row[str2] is DBNull)
                            {
                                return false;
                            }
                        }
                        double[] numArray = this.Get(r);
                        base.Crab.SetValue(this.VdField, numArray[0], r);
                        base.Crab.SetValue(this.TlsWvad.Name, numArray[1], r);
                        base.Crab.SetValue(this.TlsMWvad.Name, numArray[2], r);
                        return true;
                    }
                }
                return false;
            }
            catch
            {
                return false;
            }
        }

        public override void SetParam(string[] fields, string[] source, double[] periods, int panelID)
        {
            this.TlsWvad = base.Crab.AddLine(fields[0], panelID);
            base.Crab.SetLine(fields[0], fields[0], Color.White, 1, 0.01, 2, DashStyle.Solid);
            this.TlsMWvad = base.Crab.AddLine(fields[1], panelID);
            base.SourceDic["Close"] = source[0];
            base.SourceDic["Open"] = source[1];
            base.SourceDic["High"] = source[2];
            base.SourceDic["Low"] = source[3];
            base.SourceDic["Volume"] = source[4];
            base.IndPeriod[0].ParamValue = periods[0];
            base.IndPeriod[1].ParamValue = periods[1];
            base.Crab.DataSource.Columns.Add(this.VdField);
        }

        public LineShape TlsMWvad
        {
            get
            {
                return this.tlsMWvad;
            }
            set
            {
                this.tlsMWvad = value;
            }
        }

        public LineShape TlsWvad
        {
            get
            {
                return this.tlsWvad;
            }
            set
            {
                this.tlsWvad = value;
            }
        }

        public string VdField
        {
            get
            {
                return this.vdField;
            }
            set
            {
                this.vdField = value;
            }
        }
    }
}

